﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WC.DL.DALC.IF;
using WC.BL.BE;
using SAP.Middleware.Connector;

namespace WC.DL.DALC.SAP
{
    public class SectorDALC: ISectorDALC
    {
        #region Singleton
        public static SectorDALC Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (typeof(SectorDALC))
                    {
                        if (_instance == null)
                        {
                            _instance = new SectorDALC();
                        }
                    }
                }
                return _instance;
            }
        }

        private SectorDALC() { }
        private static volatile SectorDALC _instance = null;
        #endregion


        #region ISectorDALC Members

        public GenericBE read(string pCodigo)
        {
            IRfcFunction function = null;
            GenericBE obj = null;
            try
            {
                RfcDestination dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_005");
                if (!string.IsNullOrEmpty(pCodigo))
                {
                    function.SetValue("I_BRSCH", pCodigo);
                }
                function.Invoke(dest);
                IRfcTable T_T016T = function.GetTable("T_T016T");
                if (T_T016T != null && T_T016T.Count > 0)
                {
                    IRfcStructure S_T016T = T_T016T[0];
                    if (S_T016T != null)
                    {
                        obj = new GenericBE();
                        obj.Codigo = S_T016T["BRSCH"].GetString();
                        obj.Descripcion = S_T016T["BRTXT"].GetString();
                    }
                }
            }
            catch
            {
                obj = null;
            }
            return obj;
        }

        public List<GenericBE> list(string pCodigo, string pDescripcion)
        {
            IRfcFunction function = null;
            GenericBE obj = null; List<GenericBE> lst = new List<GenericBE>();

            RfcDestination dest;
            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_005");
                if (!string.IsNullOrEmpty(pCodigo))
                {
                    function.SetValue("I_BRSCH", pCodigo);
                }
                if (!string.IsNullOrEmpty(pDescripcion))
                {
                    function.SetValue("I_BRTXT", pDescripcion);
                }
                function.Invoke(dest);
                IRfcTable T_T016T = function.GetTable("T_T016T");
                if (T_T016T != null && T_T016T.Count > 0)
                {
                    GenericBE p = new GenericBE();
                    p.Codigo = string.Empty; p.Descripcion = "Todos";
                    lst.Add(p);
                    foreach (IRfcStructure S_T016T in T_T016T)
                    {
                        obj = new GenericBE();
                        obj.Codigo = S_T016T["BRSCH"].GetString();
                        obj.Descripcion = S_T016T["BRTXT"].GetString();

                        lst.Add(obj);
                    }
                }
            }
            catch
            {
                lst = new List<GenericBE>();
            }
            return lst;
        }

        #endregion
    }
}
